<?php

/*

    MMetaverse - Open Metaverse Access Library

    Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique

    http://www.maryjanesoft.com
    http://www.marlonj.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    Boston, MA  02111-1307  USA

    $Id$

*/

/**
 * @package mjs.mmetaverse.client
 */

/**
 * Capabilities for the client
 */
class MCapabilities {

    /**
     * Current metaverse client 
     * 
     * @var MMetaverseClient The metaverse client 
     */
    private $metaverseClient;

    /**
     * Current Capabilities
     *
     * @var string[]
     */
    private $capabilities;

    /**
     * Create a capabilities client with the client specified
     *
     * @param MMetaverseClient $metaverseClient The current client 
     */
    public function MCapabilities(MMetaverseClient $metaverseClient) {
        $this->metaverseClient = $metaverseClient;
    }

    /**
     * Request the capabilities for this client
     */
    public function requestValues($seedCapability) {
        // Create request with capabilities names
        // OpenSim can retrieve all capabilities with an empty request
        // Second Life require an array with the capabilities names 
        $request = array('NewFileAgentInventory');

        // Get capabilities 
        $response = 
            $this->metaverseClient->sendRequest($seedCapability, $request);

        // If the return is not a valid capabilities messag throw an exception 
        if(isset($response[0]['reason'])) {
            throw new Exception('Unable to load capabilities');
        }

        // Get capabilities urls 
        $this->capabilities = $response[0];
    }

    /**
     * Create a new file into agent inventory
     *
     * @param string $assetType Type of the resource
     * @param MUUID $folderID Parent Folder
     * @param string $inventoryType Type of the inventory object
     * @param string $name Name of the file
     * @param string $description Description of the file
     * @param array $content Content of the file
     */
    public function newFileAgentInventory($assetType, $inventoryType,
        MUUID $folderID, $name, $description, $content) {
        
        // Check NewFileAgentInventory Capability
        if(!isset($this->capabilities['NewFileAgentInventory'])) {
            throw new Exception('Capability NewFileAgentInventory not Found');
        }

        // Get server url
        $serverURL = $this->capabilities['NewFileAgentInventory'];        
        $llsdClient = $this->metaverseClient->getLLSDClient();

        // Create request 
        $request = array
        (
                'asset_type' => $assetType,                
                'folder_id' => $folderID,
                'inventory_type' => $inventoryType,
                'name' => $name,
                'description' => $description,
        );

        // Send the request
        $response = $llsdClient->sendRequest($serverURL, $request);

        // Get Uploader URL
        $serverURL = $response[0]['uploader'];

        // Upload file 
        $response = $llsdClient->postRawData($serverURL, $content);

        // Return the response
        return $response[0];
    }
}

?>
